package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.MyTree;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ApplyTableMapper;
import com.ruoyi.system.domain.ApplyTable;
import com.ruoyi.system.service.IApplyTableService;
import com.ruoyi.common.core.text.Convert;

/**
 * 表格管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-13
 */
@Service
public class ApplyTableServiceImpl implements IApplyTableService
{
    @Autowired
    private ApplyTableMapper applyTableMapper;

    /**
     * 查询表格管理
     *
     * @param id 表格管理主键
     * @return 表格管理
     */
    @Override
    public ApplyTable selectApplyTableById(Long id)
    {
        return applyTableMapper.selectApplyTableById(id);
    }

    /**
     * 查询表格管理列表
     *
     * @param applyTable 表格管理
     * @return 表格管理
     */
    @Override
    public List<ApplyTable> selectApplyTableList(ApplyTable applyTable)
    {
        return applyTableMapper.selectApplyTableList(applyTable);
    }

    /**
     * 新增表格管理
     *
     * @param applyTable 表格管理
     * @return 结果
     */
    @Override
    public int insertApplyTable(ApplyTable applyTable)
    {
        return applyTableMapper.insertApplyTable(applyTable);
    }

    /**
     * 修改表格管理
     *
     * @param applyTable 表格管理
     * @return 结果
     */
    @Override
    public int updateApplyTable(ApplyTable applyTable)
    {
        return applyTableMapper.updateApplyTable(applyTable);
    }

    /**
     * 批量删除表格管理
     *
     * @param ids 需要删除的表格管理主键
     * @return 结果
     */
    @Override
    public int deleteApplyTableByIds(String ids)
    {
        return applyTableMapper.deleteApplyTableByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除表格管理信息
     *
     * @param id 表格管理主键
     * @return 结果
     */
    @Override
    public int deleteApplyTableById(Long id)
    {
        return applyTableMapper.deleteApplyTableById(id);
    }

    /**
     * 查询表格管理树列表
     *
     * @return 所有表格管理信息
     */
    @Override
    public List<Ztree> selectApplyTableTree()
    {
        List<ApplyTable> applyTableList = applyTableMapper.selectApplyTableList(new ApplyTable());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (ApplyTable applyTable : applyTableList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(applyTable.getId());
            ztree.setpId(applyTable.getParentId());
            ztree.setName(applyTable.getName());
            ztree.setTitle(applyTable.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<MyTree> selectApplyTableMyTree(ApplyTable applyTable1) {
        List<ApplyTable> applyTableList = applyTableMapper.selectApplyTableList(applyTable1);
        List<MyTree> ztrees = new ArrayList<MyTree>();
        for (ApplyTable applyTable : applyTableList)
        {
            MyTree ztree = new MyTree();
            ztree.setId(applyTable.getId());
            ztree.setpId(Long.parseLong(applyTable.getParentid()));
            ztree.setName(applyTable.getName());
            ztree.setTitle(applyTable.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<MyTree> selectApplyTableMyTree(String ids) {
        String m[] = Convert.toStrArray(ids);
        List<ApplyTable> applyTableList = applyTableMapper.selectApplyTableMyTree(m);
        List<MyTree> ztrees = new ArrayList<MyTree>();
        for (ApplyTable applyTable : applyTableList)
        {
            MyTree ztree = new MyTree();
            ztree.setId(applyTable.getId());
            ztree.setpId(Long.parseLong(applyTable.getParentid()));
            ztree.setName(applyTable.getName());
            ztree.setTitle(applyTable.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }


}
